package demo

import (
	"context"
	"sync"

	"code.byted.org/bytertc/whiteboard_server/biz/api"
	"code.byted.org/bytertc/whiteboard_server/biz/dal"
	"code.byted.org/bytertc/whiteboard_server/biz/dal/spec"
	"code.byted.org/gopkg/logs/v2/log"
)

type Demo interface {
	GetNameList(ctx context.Context, unique spec.BoardUnique) (*api.DemoNameListResponse, error)
}

var once sync.Once
var d *demo

type demo struct {
	repo dal.Repo
}

func NewDemo(repo dal.Repo) Demo {
	if d == nil {
		once.Do(func() {
			d = &demo{
				repo: repo,
			}
		},
		)
	}
	return d
}

func (d *demo) GetNameList(ctx context.Context, unique spec.BoardUnique) (*api.DemoNameListResponse, error) {
	nameList, err := d.repo.GetExtraCategory(ctx, unique)
	if err != nil {
		log.V1.CtxError(ctx, "GetExtraCategory err:%+v", err)
		return nil, err
	}
	ret := &api.DemoNameListResponse{
		List: nameList,
	}
	return ret, nil
}
